package com.cbs.android.module.userkit;

import android.content.Intent;

import com.cbs.android.component.application.activity.CBSActivity;
import com.cbs.android.component.application.activity.handler.CBSActivityResultHandler;

import java.lang.ref.WeakReference;
import java.util.Hashtable;
import java.util.Map;

/**
 * Created by broche on 11/13/15.
 */
public class UserKit {
    public static final String IntentMessage = "userkitmessage";
    public static final int REQUEST_REGISTER = 0xFFFF;
    public static final int REQUEST_LOGIN = 0xFFFE;
    public static final int REQUEST_MODIFYUSERNAME = 0xFFFD;
    public static final int REQUEST_MODIFYPASSWORD = 0xFFFC;
    public static final int REQUEST_FORGOTPASSWORD = 0xFFFB;
    public static final int REQUEST_INFORMATION = 0xFFFA;
    public static final int REQUEST_QRCODE = 0xFFF9;

    private static final String TAG = UserKit.class.getName();


    private static Map<Integer, CBSActivityResultHandler> handlers = new Hashtable<>();
    private static Map<Integer, WeakReference<CBSActivity>> contexts = new Hashtable<>();
    private static MyActivityResultHandler activityResultHandler = new MyActivityResultHandler();

    public static void register(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_REGISTER)) {
            handlers.remove(REQUEST_REGISTER);
        }
        if (contexts.containsKey(REQUEST_REGISTER)) {
            contexts.remove(REQUEST_REGISTER);
        }
        handlers.put(REQUEST_REGISTER, handler);
        contexts.put(REQUEST_REGISTER, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getRegisterActivityClass());
        context.startActivityForResult(intent, REQUEST_REGISTER);
    }

    public static void login(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_LOGIN)) {
            handlers.remove(REQUEST_LOGIN);
        }
        if (contexts.containsKey(REQUEST_LOGIN)) {
            contexts.remove(REQUEST_LOGIN);
        }
        handlers.put(REQUEST_LOGIN, handler);
        contexts.put(REQUEST_LOGIN, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getLoginActivityClass());
        context.startActivityForResult(intent, REQUEST_LOGIN);
    }

    public static void modifyUserName(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_MODIFYUSERNAME)) {
            handlers.remove(REQUEST_MODIFYUSERNAME);
        }
        if (contexts.containsKey(REQUEST_MODIFYUSERNAME)) {
            contexts.remove(REQUEST_MODIFYUSERNAME);
        }
        handlers.put(REQUEST_MODIFYUSERNAME, handler);
        contexts.put(REQUEST_MODIFYUSERNAME, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getModifyUserNameActivityClass());
        context.startActivityForResult(intent, REQUEST_MODIFYUSERNAME);
    }

    public static void modifyPassword(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_MODIFYPASSWORD)) {
            handlers.remove(REQUEST_MODIFYPASSWORD);
        }
        if (contexts.containsKey(REQUEST_MODIFYPASSWORD)) {
            contexts.remove(REQUEST_MODIFYPASSWORD);
        }
        handlers.put(REQUEST_MODIFYPASSWORD, handler);
        contexts.put(REQUEST_MODIFYPASSWORD, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getModifyPasswordActivityClass());
        context.startActivityForResult(intent, REQUEST_MODIFYPASSWORD);
    }

    public static void forgotPassword(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_FORGOTPASSWORD)) {
            handlers.remove(REQUEST_FORGOTPASSWORD);
        }
        if (contexts.containsKey(REQUEST_FORGOTPASSWORD)) {
            contexts.remove(REQUEST_FORGOTPASSWORD);
        }
        handlers.put(REQUEST_FORGOTPASSWORD, handler);
        contexts.put(REQUEST_FORGOTPASSWORD, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getForgotPasswordActivityClass());
        context.startActivityForResult(intent, REQUEST_FORGOTPASSWORD);
    }

    public static void information(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_INFORMATION)) {
            handlers.remove(REQUEST_INFORMATION);
        }
        if (contexts.containsKey(REQUEST_INFORMATION)) {
            contexts.remove(REQUEST_INFORMATION);
        }
        handlers.put(REQUEST_INFORMATION, handler);
        contexts.put(REQUEST_INFORMATION, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getInformationActivityClass());
        context.startActivityForResult(intent, REQUEST_INFORMATION);
    }

    public static void qrcode(CBSActivity context, CBSActivityResultHandler handler) {
        if (handlers.containsKey(REQUEST_QRCODE)) {
            handlers.remove(REQUEST_QRCODE);
        }
        if (contexts.containsKey(REQUEST_QRCODE)) {
            contexts.remove(REQUEST_QRCODE);
        }
        handlers.put(REQUEST_QRCODE, handler);
        contexts.put(REQUEST_QRCODE, new WeakReference<>(context));
        context.addActivityResultHandler(activityResultHandler);
        Intent intent = new Intent(context, UserKitSetting.getQrCodeActivityClass());
        context.startActivityForResult(intent, REQUEST_QRCODE);
    }

    private static class MyActivityResultHandler implements CBSActivityResultHandler {
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_REGISTER
                    || requestCode == REQUEST_LOGIN
                    || requestCode == REQUEST_MODIFYUSERNAME
                    || requestCode == REQUEST_MODIFYPASSWORD
                    || requestCode == REQUEST_FORGOTPASSWORD
                    || requestCode == REQUEST_INFORMATION
                    || requestCode == REQUEST_QRCODE) {
                if (contexts.containsKey(requestCode)) {
                    CBSActivity tcontext = contexts.get(requestCode).get();
                    contexts.remove(requestCode);
                    if (tcontext != null) {
                        tcontext.removeActivityResultHandler(this);
                    }
                }

                if (handlers.containsKey(requestCode)) {
                    CBSActivityResultHandler thandler = handlers.get(requestCode);
                    handlers.remove(requestCode);
                    if (thandler != null) {
                        thandler.onActivityResult(requestCode, resultCode, data);
                    }
                }
            }
        }
    }
}
